<?php
namespace imagen;

use categoria\categoria;
use seccion\secciones;

class imagenes {
    protected $id;
    public $categoria;
    public $seccion;
    protected $filename;
    protected $titulo;
    protected $tableName;

    function __construct($id=0)
    {
        global $config;

        $this->id = 0;
        $this->categoria = new categoria();
        $this->seccion = new secciones();
        $this->filename = "";
        $this->titulo = "";
        $this->tableName = "`".$config["dbName"]."`.`imagenes`";

        if ($id!=0){
            $this->loadImagen($id);
        }
    }

    function loadImagen($id)
    {
        global $db;

        $sSql = "select img.*,cat.nombre, sec.nombre from ".$this->tableName." img " .
                "inner join ".$this->categoria->getTableName()." cat on cat.id = img.idcategoria " .
                "inner join ".$this->categoria->getTableName()." sec on sec.id = img.idseccion" .
                " where img.id = " . $id;

        $res = $db->execSql($sSql);

        if ($res){
            while ($row = mysqli_fetch_assoc($res)) {
                $this->setId($row["id"]);
                $this->setCategoria($row["idcategoria"]);
                $this->setSeccion($row["idseccion"]);
                $this->setFilename($row["filename"]);
                $this->setTitulo($row["titulo"]);
            }
        }
    }

    public function saveImage(){
        global $db;
        $rtrn = 0;

        if ($this->id == 0){
            $sSql = "insert into ".$this->tableName." (`idcategoria`,`idseccion`,`titulo`,`filename`) " .
                    "values(" . $this->categoria->getId() . "," .
                                $this->seccion->getId() . "," .
                                "'" . $this->titulo . "'," .
                                "'" . $this->filename . "')";
        }else{
            $sSql = "update ".$this->tableName." set  `idcategoria` = ".$this->getCategoria()->getId().
                                                    ",`idseccion`=".$this->getSeccion()->getId().
                                                    ",`titulo` = '".$this->getTitulo()."'".
                                                    ",`filename` = '".$this->getFilename()."'".
                    " where id = " . $this->getId();
        }

        if ($db->execSql($sSql)){
            $rtrn =  $db->getInsertedId();
        }

        return $rtrn;
    }

    public function deleteImage(){
        global $db;
        $rtrn = false;

        if ($this->getId() != 0){
            $sSql = "delete from ".$this->getTableName()." where id = ".$this->getId();
            $rtrn =  $db->execSql($sSql);
        }

        return $rtrn;

    }

    /**
     * @param \categoria\categoria $categoria
     */
    public function setCategoria($categoria)
    {
        $this->categoria->loadCategoria($categoria);
    }

    /**
     * @return \categoria\categoria
     */
    public function getCategoria()
    {
        return $this->categoria;
    }

    /**
     * @param string $filename
     */
    public function setFilename($filename)
    {
        $this->filename = $filename;
    }

    /**
     * @return string
     */
    public function getFilename()
    {
        return $this->filename;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param \seccion\secciones $seccion
     */
    public function setSeccion($seccion)
    {
        $this->seccion->loadSeccion($seccion);
    }

    /**
     * @return \seccion\secciones
     */
    public function getSeccion()
    {
        return $this->seccion;
    }

    /**
     * @param string $titulo
     */
    public function setTitulo($titulo)
    {
        $this->titulo = $titulo;
    }

    /**
     * @return string
     */
    public function getTitulo()
    {
        return $this->titulo;
    }

    /**
     * @return string
     */
    public function getTableName()
    {
        return $this->tableName;
    }



}